#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
ll c[500010],a[500010],b[500010];
ll n;
map<ll,ll> mp;

ll lowbit(ll x){
	return x&(-x);
}

void into(ll x,ll pos){
	while(pos<=n){
		c[pos]+=x;
		pos+=lowbit(pos);
	}
	return ;
}
ll getsum(ll pos){
	ll sum=0;
	while(pos>0){
		sum+=c[pos];
		pos-=lowbit(pos);
	}
	return sum;
}
void f(){
	sort(a+1,a+n+1);
	for(ll i=1;i<=n;i++){
		b[i]=mp[a[i]];
	}
}

int main()
{
	IOS;
	while(cin>>n&&n){
		mp.clear();
		for(ll i=1;i<=n;i++){
			cin>>a[i];
			mp[a[i]]=i;
		}
		f();
		for(ll i=0;i<=n;i++)c[i]=0;
		ll ans=0;
		for(ll i=1;i<=n;i++){
			into(1,b[i]);
			ll cnt=getsum(b[i]);
			ans+=(i-cnt);
			// cout<<"cnt="<<cnt<<endl;
		}
		cout<<ans<<endl;
	}	
	getchar();
	getchar();
	return 0;
}
